package zjc.bookkeeping.tools;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Selection;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import zjc.bookkeeping.R;

public class QuotaDialog extends Dialog implements View.OnClickListener {
    EditText moneyEt;
    Button sureBtn,cancelBtn;
    //获取原本金额
    public void getMoney(float money){
        moneyEt.setText(money+"");
        Selection.setSelection(moneyEt.getText(),moneyEt.getText().length());
    }

    public interface OnQuotaSureListener{
        public void onQuotaSure();
    }
    OnQuotaSureListener onQuotaSureListener;

    public void setOnQuotaSureListener(OnQuotaSureListener onQuotaSureListener) {
        this.onQuotaSureListener = onQuotaSureListener;
    }

    public QuotaDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_quota);
        initView();
    }

    private void initView() {
        moneyEt = findViewById(R.id.dialog_quota_money_et);
        sureBtn = findViewById(R.id.dialog_quota_sure_btn);
        cancelBtn = findViewById(R.id.dialog_quota_cancel_btn);
        cancelBtn.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_quota_cancel_btn:
                cancel();
                break;
            case R.id.dialog_quota_sure_btn:
                if (moneyEt.getText().toString().isEmpty() || moneyEt.getText().toString().equals("0")){
                    ToolsClass.showToast(getContext(),"请输入金额，且不能为0.", Toast.LENGTH_SHORT);
                    return;
                }
                onQuotaSureListener.onQuotaSure();
                cancel();
                break;
        }
    }
    public String getQuotaEt(){
        return moneyEt.getText().toString().trim();
    }
}
